package com.ai.nsa.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author Feng Chen 2017/7/25.
 */
public abstract class CalendarUtils {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

    private CalendarUtils() {

    }

    public static int getMonthDuration(String startDate, String endDate) throws ParseException {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();

        start.setTime(DATE_FORMAT.parse(startDate));
        end.setTime(DATE_FORMAT.parse(endDate));

        return (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12 +
                (end.get(Calendar.MONTH) - start.get(Calendar.MONTH));
    }

    public static int getMonth(String date, int amount) throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTime(DATE_FORMAT.parse(date));
        c.add(Calendar.MONTH, amount);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getMonth(String date) throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTime(DATE_FORMAT.parse(date));
        return c.get(Calendar.MONTH) + 1;
    }

}
